import { User } from '@libs/db/models/user.model';
import { Body, Controller, Delete, Get, Param, Post, Put } from '@nestjs/common';
import { ApiTags, ApiOperation, ApiProperty } from '@nestjs/swagger';
import { ModelType } from '@typegoose/typegoose/lib/types';
import { InjectModel } from 'nestjs-typegoose';

// 给 post 请求参数，设置规范
class userCreatePostDto {
    // 设置 api文档post请求的参数
    @ApiProperty({description: '用户名', example: '1摩摩可拿'})
    username: string;
    @ApiProperty({description: '密码', example: '1摩密码'})
    password: string;
}

@ApiTags('用户接口')
@Controller('users')
export class UsersController {
  constructor(
    @InjectModel(User) private readonly UserMode: ModelType<User>
  ){}

  @Get()
  @ApiOperation({summary: '用户列表'})
  async select() {
    return await this.UserMode.find();
  }

  @Post()
  @ApiOperation({summary: '创建用户'})
  async create(@Body() createPostDto: userCreatePostDto ) {
    await this.UserMode.create(createPostDto);
    return {
      success: '创建成功！',
      data: createPostDto
    }
  }

  @Put(':id')
  @ApiOperation({summary: '修改用户'})
  async update(@Param('id') id: string, @Body() createPostDto: userCreatePostDto) {
    await this.UserMode.updateOne({_id: id}, createPostDto);
    return {
      success: true
    }
  }

  @Delete(':id')
  @ApiOperation({summary: '删除用户'})
  async remove(@Param('id') id: string) {
    await this.UserMode.findOneAndDelete({_id: id})
    return {
      success: true
    }
  }

}
